package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.UserBean;

import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserMapper mapper;


    @Override
    public UserBean land(String userName, String password) {
        UserBean userBean = mapper.land(userName,password);
        return userBean;
    }

    @Override
    public List<UserBean> findAll() {
        return mapper.selectList(null);
    }

    @Override
    public boolean del(Integer id, UserBean loginUser) {

        if (loginUser.getId()==mapper.selectById(id).getId()){
            return false;
        }{
           mapper.deleteById(id);
            return true;
        }

    }

    @Override
    public UserBean findById(Integer id) {
        return mapper.selectById(id);
    }

    @Override
    public void updateGrade(Integer id, String grade) {
        UserBean user = new UserBean();
        user.setId(id);
        user.setGrade(grade);
        mapper.updateById(user);

    }

    @Override
    public void updatePassword(Integer id, String password) {
        UserBean user = new UserBean();
        user.setId(id);
        user.setPassword(password);
        mapper.updateById(user);
    }

    @Override
    public boolean add(UserBean user) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("t_userAccount",user.getUserName());
        if (mapper.selectOne(qw)==null){
            mapper.insert(user);
            return true;
        }else {
            return false;
        }
    }


}
